// pages/my/my.js
import { ShiftModel } from '../../models/shift'
import { OverTimeModel } from '../../models/overtime'
var wxCharts = require('../../util/wxcharts-min.js')
var pieChart = null
const shift = new ShiftModel()
const overtime = new OverTimeModel()
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    isShow:0,
    isShowChart:0,
    showShift:false,
    weekdates:null,
    backColor:null,
    arrowColor:null,
    ms:null,
    mds:null,
    es:null,
    rest:null,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  SetShift: function() {
    const wk = this.methods._getWeekDates()
    const show = this.data.isShow
    const showChart = this.data.isShowChart
    this.setData({
      weekdates:wk,
      isShow:show==0?1:0,
      isShowChart:showChart==0?0:0,
      backColor:"gray",
    })
  },

  ShowCharts: async function() {
    let wid = wx.getStorageSync('wid') 
    const showChart = this.data.isShowChart
    const show = this.data.isShow
    this.setData({
      isShowChart: showChart==0?1:0,
      isShow:show==0?0:0,
    })
    const wks = await shift.getWeekClass(wid)
    let result = wks.result
    let counts = this.methods._getShiftCount(result)
    let ms = counts[0]
    let mds = counts[1]
    let es = counts[2]
    let rest = counts[3] 
    this.setData({
      ms:ms,
      mds:mds,
      es:es,
      rest:rest,
      showShift:true
    })
    const showShift = this.data.showShift
    if(showShift){
    var windowWidth = 320;
        try {
            var res = wx.getSystemInfoSync();
            windowWidth = res.windowWidth;
        } catch (e) {
            console.error('getSystemInfoSync failed!');
        }
   
      pieChart = new wxCharts({
        animation: true,
        canvasId: 'pieCanvas',
        type: 'pie',
        series: [{
          name: '早班',
          data: this.data.ms,
        }, {
          name: '中班',
          data: this.data.mds,
        }, {
          name: '晚班',
          data: this.data.es,
        }, {
          name: '休息',
          data: this.data.rest,
        },],
        width: windowWidth,
        height: 300,
        dataLabel: true,
      })
    };
  },
  
  AddShift: async function (event) {
    const wid = wx.getStorageSync('wid')
    const day = event.detail.date
    let date = this.methods._datetodate(day)
    const shift_type = event.detail.shift_type || '早班'
    const ot = event.detail.overtime
    
    const Result = await shift.addOneShift(wid,shift_type,date)
    const result = Result.result
    if (result=="success") {
      const r = await overtime.addOneOverTime(wid,ot,date)
      console.log(r)
      wx.showToast({
        title: '新建成功',
        icon: 'success',
        duration: 1000,
      })
      if (result=="fail") {
        wx.showToast({
          title: '班次已存在',
          duration: 2000,
          icon:'none'
        })
      }
    } 
  },

  UpdateShift: async function(event) {
    const wid = wx.getStorageSync('wid')
    const day = event.detail.date
    let date = this.methods._datetodate(day)
    const shift_type = event.detail.shift_type || '早班'
    const ot = event.detail.overtime
    
    const Result = await shift.updateOneShift(wid,shift_type,date)
    const result = Result.result
    await overtime.updateOneOverTime(wid,ot,date)
    if(result=="success"){
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1000
      })
    }
    if(result=="fail"){
      wx.showToast({
        title: '班次不存在,请先新建',
        duration: 2000,
        icon:'none'
      })
    }    
  },

  OnLatest: function(event) {
    const wk = this.methods._getWeekDates()
    this.setData({
      weekdates:wk,
      backColor:"gray",
      arrowColor:"black"
    })
  },

  OnNext: function(event) {
    const wk = this.methods._getNextWeekDates()
    this.setData({
      weekdates: wk,
      backColor: "black",
      arrowColor: "gray"
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      active: "my"
    })
  }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  
  methods: {
   
    _getWeekDates: function () {
      let weekdates = []
      let now = new Date()
      let day = now.getDay() || 7
      for(let i=0;i<7;i++){
        const v = new Date(now.getFullYear(),now.getMonth(),now.getDate()+1-day+i)
        const weekdate = v.getMonth()+1+'月'+v.getDate()+'日'
        weekdates.push(weekdate)        
      }
      return weekdates      
    },

    _getNextWeekDates: function () {
      let weekdates = []
      let now = new Date()
      let day = now.getDay() || 7
      for(let i=0;i<7;i++){
        const v = new Date(now.getFullYear(),now.getMonth(),now.getDate()+1+7-day+i)
        const weekdate = v.getMonth()+1+'月'+v.getDate()+'日'
        weekdates.push(weekdate)        
      }
      return weekdates      
    },

    _datetodate (day) {
      let now = new Date()
      let day1 = day.replace(/月/,"-")
      let day2 = day1.replace(/日/,"")
      const v = now.getFullYear()+'-'+day2
      return v
    },

    _getShiftCount (result) {
      let ms=0,mds=0,es=0,rest=0
      let counts
      for(let i=0;i<7;i++){
        let shiftType = result[i].shift_type
        switch (shiftType) {
          case '早班':
            ms++;
            break;
          case '中班':
            mds++;
            break;
          case '晚班':
            es++;
            break;
          case '休息':
            rest++;
            break;
          default:
            break;
        }
      }
      return counts = [ms,mds,es,rest]
      
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})